package com.lykj.modules.shop.controller;

import com.lykj.common.cache.CateCache;
import com.lykj.core.cache.utils.CacheUtil;
import com.lykj.core.secure.LykjUser;
import com.lykj.core.tool.support.Kv;
import com.lykj.modules.shop.wrapper.StoreCategoryWrapper;
import io.swagger.annotations.*;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import lombok.AllArgsConstructor;

import javax.validation.Valid;

import com.lykj.core.mp.support.Condition;
import com.lykj.core.mp.support.Query;
import com.lykj.core.tool.api.R;
import com.lykj.core.tool.utils.Func;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lykj.modules.shop.entity.StoreCategory;
import com.lykj.modules.shop.vo.StoreCategoryVO;
import com.lykj.modules.shop.service.StoreCategoryService;
import com.lykj.core.boot.ctrl.LykjController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.Map;

/**
 * 商品分类表 控制器
 *
 * @author cxl
 * @since 2022-01-13
 */
@RestController
@AllArgsConstructor
@RequestMapping("lykj-shop/store/category")
@Api(value = "商品分类", tags = "商品分类接口")
public class StoreCategoryController extends LykjController {

	private final StoreCategoryService storeCategoryService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入storeCategory")
	public R<StoreCategory> detail(StoreCategory storeCategory) {
		StoreCategory detail = storeCategoryService.getOne(Condition.getQueryWrapper(storeCategory));
		return R.data(detail);
	}

	/**
	 * 分页 商品分类表
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入storeCategory")
	public R<IPage<StoreCategory>> list(StoreCategory storeCategory, Query query) {
		IPage<StoreCategory> pages = storeCategoryService.page(Condition.getPage(query), Condition.getQueryWrapper(storeCategory));
		return R.data(pages);
	}

	/**
	 * 懒加载列表
	 */
	@GetMapping("/lazy-list")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "cateName", value = "分类名称", paramType = "query", dataType = "string")
	})
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "懒加载列表", notes = "传入cate")
	public R<List<StoreCategoryVO>> lazyList(@ApiIgnore @RequestParam Map<String, Object> cate, Long parentId, LykjUser user) {
		List<StoreCategoryVO> list = CateCache.getStoreCategoryList(user.getTenantId(), parentId, cate);
		return R.data(StoreCategoryWrapper.build().listNodeLazyVO(list));
	}

	/**
	 * 自定义分页 商品分类表
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入storeCategory")
	public R<IPage<StoreCategoryVO>> page(StoreCategoryVO storeCategory, Query query) {
		IPage<StoreCategoryVO> pages = storeCategoryService.selectStoreCategoryPage(Condition.getPage(query), storeCategory);
		return R.data(pages);
	}

	/**
	 * 新增 商品分类表
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入storeCategory")
	public R save(@Valid @RequestBody StoreCategory storeCategory) {
		CacheUtil.clear("lykj:shop:cate");
		CacheUtil.clear("lykj:shop:cate", Boolean.FALSE);
		return R.status(storeCategoryService.save(storeCategory));
	}

	/**
	 * 修改 商品分类表
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入storeCategory")
	public R update(@Valid @RequestBody StoreCategory storeCategory) {
		CacheUtil.clear("lykj:shop:cate");
		CacheUtil.clear("lykj:shop:cate", Boolean.FALSE);
		return R.status(storeCategoryService.updateById(storeCategory));
	}

	/**
	 * 新增或修改 商品分类表
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入storeCategory")
	public R submit(@Valid @RequestBody StoreCategory storeCategory) {
		if (storeCategoryService.submit(storeCategory)) {
			CacheUtil.clear("lykj:shop:cate");
			CacheUtil.clear("lykj:shop:cate", Boolean.FALSE);
			// 返回懒加载树更新节点所需字段
			Kv kv = Kv.create().set("id", String.valueOf(storeCategory.getId()));
			return R.data(kv);
		}
		return R.fail("操作失败");
//		return R.status(storeCategoryService.submit(storeCategory));
	}

	/**
	 * 删除 商品分类表
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		CacheUtil.clear("lykj:shop:cate");
		CacheUtil.clear("lykj:shop:cate", Boolean.FALSE);
		return R.status(storeCategoryService.deleteLogic(Func.toLongList(ids)));
	}

	/**
	 * 获取商品分类树形结构
	 */
	@GetMapping("/tree")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "树形结构", notes = "树形结构")
	public R<List<StoreCategoryVO>> tree(String tenantId, LykjUser lykjUser) {
		if(lykjUser!=null){
			tenantId = lykjUser.getTenantId();
		}
		List<StoreCategoryVO> tree = CateCache.getStoreCategoryTree(tenantId);
//		List<StoreCategoryVO> tree = StoreCategoryCache.getStoreCategoryTree(Func.toStrWithEmpty(tenantId, lykjUser.getTenantId()));//storeCategoryService.tree(Func.toStrWithEmpty(tenantId, lykjUser.getTenantId()));
		return R.data(tree);
	}

}
